layui.define(["jquery"],function(exports){
    var $ = layui.jquery,
        conf = layui.conf,
        device = layui.device(),
        dwidth = $(window).width() < 1024 ? true : false;
    if (device.weixin || device.ios || device.android || dwidth) {
        $('.layadmin-flexible i').removeClass("layui-icon-shrink-right");
    }else{
        $('.layadmin-flexible i').removeClass("layui-icon-spread-left");
    }
    $('.layadmin-body-shade').hide();//隐藏遮罩
    self = {}
    //显示和隐藏菜单
    self.navigate = function(elem){
        if (device.weixin || device.ios || device.android || dwidth) {
            //mobile layadmin-side-spread-sm
            $('.layadmin-tabspage-none').toggleClass("layadmin-side-spread-sm");
            if($('.layadmin-tabspage-none').hasClass("layadmin-side-spread-sm")){
                $('.layadmin-body-shade').show();
            }else{
                $('.layadmin-body-shade').hide();
            }
            
        }else{
            //PC layadmin-side-shrink
            $('.layadmin-tabspage-none').toggleClass("layadmin-side-shrink");
        }
        $('.layadmin-flexible i').toggleClass('layui-icon-shrink-right');
        $('.layadmin-flexible i').toggleClass('layui-icon-spread-left');

    }
    //隐藏菜单和遮罩
    self.shade = function(elem){
        if (device.weixin || device.ios || device.android || dwidth) {
            //移动端
            $('.layadmin-tabspage-none').removeClass("layadmin-side-spread-sm");
        }else{
            //PC端
            $('.layadmin-tabspage-none').removeClass("layadmin-side-shrink");
        }
        $(this).hide();
        $('.layadmin-flexible i').toggleClass('layui-icon-shrink-right');
        $('.layadmin-flexible i').toggleClass('layui-icon-spread-left');
    }
    //修正按钮图标
    self.flexible_icon = function(){

    }
    //合并AJAX对象
    self.createRequestParams = function(params) {
        var defaultArgs = {
            timeout:3000,
            async: false,//禁止异步
            type:"GET",
            dataType:"JSON",
        }
        return $.extend(defaultArgs,params);
    }
    self.socketRequestParams = function(params, elem=null){
        var socketArgs = {
            timeout: 6000,
            //async: false,//禁止异步
            type: "GET",
            dataType: "JSON",
            url: "/cli/soc",
            success: function(data){
                if(data == "succ"){
                    layer.msg("操作成功");
                }else{
                    layer.msg("操作失败");
                }
                if(elem != null) self.btn_enable(elem);
            },
            error: function(){
                if(elem != null) self.btn_enable(elem);
            }
        }
        return $.extend(socketArgs,params);
    }
    //发起AJAX请求
    self.request = function(params) {
        params = self.createRequestParams(params);
        $.ajax(params);
    }
    self.doSocket = function(params, elem=null){
        params = self.socketRequestParams(params, elem);
        $.ajax(params);
    }
    //填充html到selector容器
    self.fill_html = function(selector,html){
        $(selector).empty();
        $(selector).html(html);
        //document.getElementById(selector).innerHTML = html;
    }
    //修正面包屑导航
    self.fix_breadcrumb = function(params) {
        $(".crumbs cite").html(params);//修改面包屑导航
    }
    //修正当前页标题
    self.fix_document_title = function(title){
        $("title").html(conf.siteName + " - " + title);//修改页面标题
    }
    //侧边栏控制
    self.sidebar_ctrl = function(ctrl){
        if(ctrl == "hidden"){
            $('.layadmin-tabspage-none').removeClass("layadmin-side-spread-sm");//隐藏左侧菜单
            $('.layadmin-tabspage-none').removeClass("layadmin-side-shrink");//放大左侧菜单
            $('.layadmin-body-shade').hide();//隐藏遮罩
        }
        if(ctrl == "show"){
            $('.layadmin-tabspage-none').addClass("layadmin-side-spread-sm");//移动端左侧菜单
            $('.layadmin-tabspage-none').addClass("layadmin-side-shrink");//缩小左侧菜单
            $('.layadmin-body-shade').show();//显示遮罩
        }
    }
    self.btn_disable = function(elem){
        elem.attr("disabled",true).addClass("layui-btn-disabled");//将按钮设置为不可用状态，防止客户端连续点击
    }
    self.btn_enable = function(elem){
        elem.attr("disabled",false).removeClass("layui-btn-disabled");//恢复按钮为可点击状态
    }
    //日期控件
    //laydate.render({
    //    elem: '#birthday', //指定元素
    //    type: 'datetime',  //控件选择类型
    //    range: true,        //定义分隔符，默认'-'
    //    format: 'yyyy-MM-dd HH:mm:ss'
    //});
    exports("view",self);
})